﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Data;

public partial class ControlUser_ControlMaHangQuetKiemSoatSauXN : System.Web.UI.UserControl
{
    MethodClassCommon common;
   
    private string str = BCDataCenter.Configuration.CurrentConnectionString;
  
    clsObj Obj;
    clsSql Sql = new clsSql();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fload_ChiNhanh();
           
        }
    }

    void fload_ChiNhanh()
    {

        Obj = new clsObj();
        Obj.Parameter = new string[] { };
        Obj.ValueParameter = new object[] {  };
        Obj.SpName = "ls_getDistributorList";
        Sql.fGetData(Obj);
        ddlChiNhanh.DataSource = Obj.Dt;
        ddlChiNhanh.DataValueField = "siteCode";
        ddlChiNhanh.DataTextField = "siteName";
        ddlChiNhanh.DataBind();
    }

   
    protected void txtMaHang_TextChanged(object sender, EventArgs e)
    {
        common = new MethodClassCommon();
        lblKetQua.Visible = false;
        try
        {

            if (ddlChiNhanh.SelectedValue == "")
            {
                uscMsgBox1.AddMessage("Mã chi nhánh không được rỗng", uscMsgBox.enmMessageType.Error);
                return;
            }
            Fload_TenHang();
            #region Insert Du Lieu
           
            Obj = new clsObj();
            Obj.Parameter = new string[] { "@userName","@siteCode", "@itemKey" };
            Obj.ValueParameter = new object[] {Session["username"].ToString(), ddlChiNhanh.SelectedValue, txtMaHang.Text };
            Obj.SpName = "ls_addScannedItem";
            Sql.fGetData(Obj);
            #endregion
            Fload_Luoi_SauKhiLuu();
        }
        catch
        {
            
        }
    }

    private void Fload_TenHang()
    {
            Obj = new clsObj();
            Obj.Parameter = new string[] { "@siteCode", "@itemKey" };
            Obj.ValueParameter = new object[] { ddlChiNhanh.SelectedValue, txtMaHang.Text };
            Obj.SpName = "ls_getItem4LaterScanned";
            Sql.fGetData(Obj);
            if (Obj.Dt.Rows.Count > 0)
            {
                txtTenHang.Text = Obj.Dt.Rows[0]["Description"].ToString();
            }
            else
            {
                uscMsgBox1.AddMessage("Mã  hàng không đúng,nhập lại", uscMsgBox.enmMessageType.Error);
                txtTenHang.Text = "";
                txtMaHang.Text = "";
            }
        
      
    }
    protected void ddlChiNhanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fload_Luoi();
    }

    private void Fload_Luoi()
    {

        Obj = new clsObj();
        Obj.Parameter = new string[] { "@userName", "@siteCode"};
        Obj.ValueParameter = new object[] { Session["username"].ToString(), ddlChiNhanh.SelectedValue };
        Obj.SpName = "ls_getLaterScannedItemList";
        Sql.fGetData(Obj);
        GridView1.DataSource = Obj.Dt;
        GridView1.DataBind();
     
        
    }

    private void Fload_Luoi_SauKhiLuu()
    {

        Obj = new clsObj();
        Obj.Parameter = new string[] { "@userName", "@siteCode", "@isLoad" };
        Obj.ValueParameter = new object[] { Session["username"].ToString(), ddlChiNhanh.SelectedValue, 1 };
        Obj.SpName = "ls_getLaterScannedItemList";
        Sql.fGetData(Obj);
        GridView1.DataSource = Obj.Dt;
        GridView1.DataBind();


    }
 
   protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        Fload_Luoi();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       // 6.	ls_removeScannedItem @userName varchar(50),@itemKey varchar(50)
        string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
        Obj = new clsObj();
        Obj.Parameter = new string[] { "@userName", "@itemKey" };
        Obj.ValueParameter = new object[] { Session["username"].ToString(),keys};
        Obj.SpName = "ls_removeScannedItem";
        Sql.fNonGetData(Obj);
        Fload_Luoi_SauKhiLuu();
      
    }
   
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        #region Insert Du Lieu

        Obj = new clsObj();  //@userName varchar(50), @siteCode varchar(20)
        Obj.Parameter = new string[] { "@userName", "@siteCode" };
        Obj.ValueParameter = new object[] { Session["username"].ToString(), ddlChiNhanh.SelectedValue };
        Obj.SpName = "ls_saveScannedItemList";
        Sql.fGetData(Obj);
        #endregion
    }
  
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Fload_Luoi();
    }
}
